package com.qidian.common.beanCache;

import java.util.Iterator;
import java.util.concurrent.ConcurrentHashMap;

import com.qidian.common.beans.ClientBean;

/**
 * 客户端信息缓冲区
 * 
 * */
public class ClientBeanCache
{
	private static ClientBeanCache clientBeanCache = new ClientBeanCache() ;
	
	/* 所有的客户端信息:<客户端ID,客户端>*/
	private ConcurrentHashMap<Long,ClientBean> buffer ;
	
	
	public ConcurrentHashMap<Long,ClientBean> getBuffer()
	{
		return buffer ;
	}
//	// 活动客户端列表
//	private ArrayList<ClientBean> activeList ;
//	// 当前的活动客户端位置
//	private int currentActivePosition = 0 ;
//	
	private ClientBeanCache()
	{
		buffer = new ConcurrentHashMap<Long,ClientBean>() ;
//		activeList = new ArrayList<ClientBean>() ;
	}
	public static ClientBeanCache getInstance()
	{
		return clientBeanCache ;
	}
	
	public boolean put(ClientBean client)
	{
		buffer.put(client.getClientId(), client) ;
		return true ;
	}
	public void removeClient(Long id)
	{
		buffer.remove(id) ;
	}
	public ClientBean getClient(Long id)
	{
		return buffer.get(id) ;
	}
	public ClientBean getClient(String strId)
	{
		return buffer.get(Long.parseLong(strId)) ;
	}
	public ClientBean getClientByName(String userName)
	{
		Iterator<ClientBean> clients = buffer.values().iterator() ;
		ClientBean client ;
		while(clients.hasNext())
		{
			client = clients.next() ;
			if (client.getClientName().equalsIgnoreCase(userName))
			{
				return client ;
			}
		}
		return  null ;
	}
	/**
	 * 客户端登陆，将客户端添加到活动列表.
	 * 成功，返回已登录的客户端对象，否则返回null
	 * 
	 * */
	public ClientBean login(String userName, String password)
	{
		ClientBean client = getClientByName(userName);
		if (client == null)
			return null ;
		
		if (client.getPassword().equals(password))
		{
			if(client.isDeleted())
				return null ;
			return client ;
		}
		
		return null ;
	}
//	/**
//	 * 添加至活动客户端列表
//	 * 
//	 * */
//	public void addActive(ClientBean client)
//	{
//        client.setStatus(ClientStatus.Active) ;
//        if (!activeList.contains(client))
//            activeList.add(client) ;
//        client.setLastActiveTime(System.currentTimeMillis()) ;
//	}
//	/**
//	 * 将客户端从活动列表中删除
//	 * 
//	 * */
//	public void removeActive(ClientBean client)
//	{
//	    client.setNoActive() ;
//	    activeList.remove(client) ;
//	    return ;
//	}

//	/**
//	 * 返回下一个活动的客户端。
//	 * 如果没有活动的客户端，返回null
//	 * 
//	 * */
//	public ClientBean getNextActiveClient()
//	{
//		int count = 0 ;
//		ClientBean client = null ;
//		while(count < activeList.size())
//		{
//		    if (currentActivePosition >= activeList.size())
//	            currentActivePosition = 0 ;
//		    
//			client = activeList.get(currentActivePosition) ;
//			currentActivePosition ++;
//            count ++ ;
//            
//			if (!client.isZombie() && client.isActive())
//				return client ;
//		}
//		return null;
//	}

	/**
	 * 添加客户端
	 * 
	 * */
	public void add(ClientBean clientBean) 
	{
		buffer.put(clientBean.getClientId(), clientBean) ;
	}
	
//	public ArrayList<ClientBean> getActiveClientList()
//	{
//	    return activeList ;
//	}
	
	public String toString()
	{
		StringBuffer sb = new StringBuffer(1024);
		sb.append("Client count:").append(buffer.size()).append(", Active client count:") ;
		sb.append("\n") ;
		Iterator<ClientBean> clientItr =  buffer.values().iterator(); 
		while(clientItr.hasNext())
		{
			ClientBean client = clientItr.next() ;
			sb.append("id:").append(client.getClientId()).append(",status:").append(client.getStatus()) ;
		}
		return sb.toString() ;
	}
	public Object size() 
	{
		return buffer.size();
	}
}
